package com.sky.controller.admin;

import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;

/**
 * @Author: QingYingYan
 * @Date: 2025-10-26 23:42
 * @Description:
 */
@Api(tags = "店铺操作接口")
@RequestMapping("/admin/shop")
@Slf4j
@RestController
public class ShopController {



    @Autowired
    private RedisTemplate redisTemplate;

    @PostConstruct
    public void initShopStatus() {
        // 检查Redis中是否已存在SHOP_STATUS，如果不存在则设置默认值为1
        if (redisTemplate.opsForValue().get("SHOP_STATUS") == null) {
            redisTemplate.opsForValue().set("SHOP_STATUS", 1);
            log.info("初始化SHOP_STATUS为1");
        }
    }

    @GetMapping("/status")
    @ApiOperation("获取营业状态")
    public Result getStatus() {
        Integer shopStatus = (Integer) redisTemplate.opsForValue().get("SHOP_STATUS");
        return Result.success(shopStatus);
    }

    @PutMapping("/{status}")
    @ApiOperation("设置营业状态")
    public Result setStatus(@PathVariable Integer status) {
        // 应该根据传入的 status 值来设置营业状态
        log.info("设置营业状态"+status);
        Integer shopStatus = (Integer) redisTemplate.opsForValue().get("SHOP_STATUS");
        System.out.println("设置营业状态=========================="+shopStatus);
        if (!shopStatus.equals(status)) {
            redisTemplate.opsForValue().set("SHOP_STATUS", status);
        }
        return Result.success(status);
    }
}
